package com.qianyitian.hope2.spider.fetcher;

import com.github.tusharepro.core.entity.AdjFactorEntity;
import com.qianyitian.hope2.spider.external.TuShareClient;
import com.qianyitian.hope2.spider.model.AdjFactorInfo;
import com.qianyitian.hope2.spider.model.Stock;
import com.qianyitian.hope2.spider.model.StockAdjFactor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("tuShareWebStockRetreiver")
public class TuShareWebStockRetreiver extends WebStockRetreiver {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    TuShareClient tuShareClient;

    public StockAdjFactor getSimpleAdjFactor(Stock stock) throws IOException {
        List<AdjFactorEntity> adjFactorNow = tuShareClient.getAdjFactor(stock.getCode());
        Map<Double, AdjFactorEntity> collect = adjFactorNow.stream().collect(Collectors.toMap(bar -> bar.getAdjFactor(), bar -> bar, (v1, v2) -> v2, LinkedHashMap::new));

        List<AdjFactorInfo> adjFactorSimpleList = collect.values().stream().map(adj -> {
            AdjFactorInfo info = new AdjFactorInfo();
            info.setDate(adj.getTradeDate());
            info.setAdjFactor(adj.getAdjFactor());
            return info;
        }).collect(Collectors.toList());
        //从小到大排序
        java.util.Collections.reverse(adjFactorSimpleList);

        StockAdjFactor stockAdjFactor = new StockAdjFactor();
        stockAdjFactor.setCode(stock.getCode());
        stockAdjFactor.setName(stock.getName());
        stockAdjFactor.setAdjList(adjFactorSimpleList);
        return stockAdjFactor;
    }

    @Override
    public Stock fetchStockInfo(Stock code) throws IOException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public Stock fetchIndexInfo(Stock code) throws IOException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public String fetchFundsInfo(String code) throws IOException {
        throw new RuntimeException("not implemented");
    }
}
